﻿using System;
using System.Collections.Generic;
using System.Xml;
using TimetableCSP.Scheduler.Entities;

namespace TimetableCSP.Scheduler
{
	/// <summary>
	/// Provides methods for retrieving data
	/// </summary>
	public class DataProvider
	{
		/// <summary>
		/// Gets a list of classes being taught.
		/// </summary>
		/// <returns>A list of classes.</returns>
		public static IEnumerable<ClassVariable> GetClasses()
		{
			List<ClassVariable> result = new List<ClassVariable>();
			//
			// Load the XML document containing the list of classes from an embedded resource 
			//
			XmlDocument classesXml = new XmlDocument();
			classesXml.LoadXml(Properties.Resources.Classes);
			int counter = 1;
			//
			// Iterate through the <Class> elements in the XML document and concstruct a ClassVariable object for each
			//
			foreach (XmlNode classNode in classesXml.DocumentElement.ChildNodes)
			{
				ClassVariable classVariable = new ClassVariable();
				classVariable.Id = counter++;
				foreach (XmlNode propertyNode in classNode.ChildNodes)
				{
					switch (propertyNode.Name)
					{
						case "Course":
							if (string.IsNullOrEmpty(classVariable.Name))
							{
								classVariable.CourseId = propertyNode.InnerText;
							}
							else
							{
								classVariable.CourseId = propertyNode.InnerText + " " + classVariable.Name;
							}
							break;
						case "Name":
							if (string.IsNullOrEmpty(classVariable.Name))
							{
								classVariable.Name = propertyNode.InnerText;
							}
							else
							{
								classVariable.Name += " " + propertyNode.InnerText;
							}
							break;
						case "Teacher":
							classVariable.Teacher = propertyNode.InnerText;
							break;
						case "NumberOfStudents":
							classVariable.NumberOfStudents = Convert.ToInt32( propertyNode.InnerText );
							break;
						case "Group":
							classVariable.Groups.UnionWith( propertyNode.InnerText.Split(',') );
							break;
					}
				}
				result.Add(classVariable);
			}
			return result;
		}

		/// <summary>
		/// Gets a list of classrooms available.
		/// </summary>
		/// <returns>A list of classrooms.</returns>
		public static IEnumerable<Classroom> GetClassrooms()
		{
			List<Classroom> result = new List<Classroom>();
			//
			// Load the XML document containing the list of classrooms from an embedded resource 
			//
			XmlDocument classRoomsXml = new XmlDocument();
			classRoomsXml.LoadXml(Properties.Resources.Classrooms);
			//
			// Iterate through the <Classroom> elements in the XML document and concstruct a Classroom object for each
			//
			foreach (XmlNode classRoomNode in classRoomsXml.DocumentElement.ChildNodes)
			{
				Classroom classRoom = new Classroom();
				foreach (XmlNode propertyNode in classRoomNode.ChildNodes)
				{
					switch (propertyNode.Name)
					{
						case "Name":
							if (string.IsNullOrEmpty(classRoom.Name))
							{
								classRoom.Name = propertyNode.InnerText;
							}
							else
							{
								classRoom.Name += " " + propertyNode.InnerText;
							}
							break;
						case "Capacity":
							classRoom.Capacity = Convert.ToInt32(propertyNode.InnerText);
							break;
					}
				}
				result.Add(classRoom);
			}
			return result;
		}
	}
}
